#!/bin/bash

#
# [AUTHOR]  : Babil (Golam Sarwar)
# [VERSION] : 0.1
#
# [DOCUMENTATION]: 
#
# This scripts shows min, max, average and standard
# deviation from a specified column of a specified 
# file. For example, if 'file.dat' contains:
#
# 1 3
# 2 9
# 3 7
# 4 5
# 5 10
#
# $ stats.sh file.dat 2
#
# [*] input      : file.dat
# [*] column     : 2
# [*] no of data : 5
#
# [*] avg        : 6.8
# [*] min        : 3
# [*] max        : 10
# [*] std.dev    : 2.56125
#

function usage()
{
	echo
	echo "[*] usage: `basename $0` filename column"
	echo
	exit
}

if [ -z $1 ] || [ -z $2 ]
then
	usage
fi

line=`cat $1 | wc -l`
max=`cat $1 | awk -v c=$2 '{if($c>=s){s=$c;line=NR}}END{print s}'`
min=`cat $1 | awk -v s=9999999999 -v c=$2 '{if($c<=s){s=$c;line=NR};}END{print s}'`
avg=`cat $1 | awk -v c=$2 '{total=total+$c}END{print total/NR}'`
stdev=`cat $1 | awk -v avg=$avg -v c=$2 '{x=$c-avg;total=total+x*x;}END{print sqrt(total/NR)}'`

echo
echo "[*] input      : $1"
echo "[*] column     : $2"
echo "[*] no of data : $line"
echo
echo "[*] avg        : $avg"
echo "[*] min        : $min"
echo "[*] max        : $max"
echo "[*] std.dev    : $stdev"
echo

